home *** CD-ROM | disk | FTP | other *** search
/ A.C.E. 2 / ACE CD 2.iso / FILES / UTILS / GAMESDS2.DMS / in.adf / GDS_Tutorial.lha / Examples / picture / view.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-15  |  16.3 KB  |  500 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include <exec/types.h>
  5. #include <intuition/intuition.h>
  6. #include <intuition/intuitionbase.h>
  7. #include <graphics/gfx.h>
  8. #include <clib/exec_protos.h>
  9. #include <clib/intuition_protos.h>
  10. #include <clib/graphics_protos.h>
  11. #include <pragmas/exec_sysbase_pragmas.h>
  12. #include <pragmas/intuition_pragmas.h>
  13. #include <pragmas/graphics_pragmas.h>
  14.  
  15. #include "GameSmith:include/libraries/libraries.h"
  16. #include "GameSmith:include/libraries/libptrs.h"
  17. #include "GameSmith:GameSmith.h"
  18.  
  19. #define FONT_WIDTH   8
  20. #define FONT_HEIGHT   8
  21.  
  22. #define CUSTOM_FLAGS   (BORDERLESS|SUPER_BITMAP|ACTIVATE|NOCAREREFRESH|GIMMEZEROZERO|WINDOWCLOSE)
  23. #define WB_FLAGS      (SUPER_BITMAP|ACTIVATE|WINDOWSIZING|WINDOWDEPTH|WINDOWCLOSE|WINDOWDRAG|GIMMEZEROZERO|NOCAREREFRESH)
  24.  
  25. /* ----------------------------------------------------------------------- */
  26. /* Function prototypes:                                                    */
  27.  
  28. int get_wb_info(void);
  29. int load_image(char *);
  30. int display_image(char *);
  31. int display_image_window(char *);
  32. int display_image_screen(char *);
  33.  
  34. /* ----------------------------------------------------------------------- */
  35.  
  36. int left,right,top,bottom,barheight,width,height,depth;
  37. unsigned long color[256];         /* color table */
  38.  
  39. char screen_title[]="GameSmith® IFF ILBM Loader";
  40.  
  41. char topaz8_text[]="topaz.font";
  42.  
  43. struct TextAttr topaz8 =
  44.    {
  45.    topaz8_text,                  /* Addr of ASCIIZ string containing font name */
  46.    8,                              /* font YSize (height) */
  47.    FS_NORMAL,                     /* style flags */
  48.    FPF_DESIGNED|FPF_ROMFONT      /* flags */
  49.    };
  50.  
  51. unsigned short pens[] = {0xffff};   /* needed for new (3d) look */
  52.  
  53. struct TagItem tags[] = {
  54.    {
  55.    SA_Pens,
  56.    (unsigned long)pens
  57.    },
  58.    {
  59.    SA_Width,
  60.    STDSCREENWIDTH
  61.    },
  62.    {
  63.    SA_Height,
  64.    STDSCREENHEIGHT
  65.    },
  66.    {
  67.    SA_Depth,
  68.    0L
  69.    },
  70.    {
  71.    SA_AutoScroll,
  72.    1L
  73.    },
  74.    {
  75.    SA_Type,
  76.    CUSTOMSCREEN
  77.    },
  78.    {
  79.    SA_Quiet,
  80.    1L
  81.    },
  82.    {
  83.    SA_Overscan,
  84.    OSCAN_TEXT
  85.    },
  86.    {
  87.    TAG_DONE,
  88.    0L
  89.    }
  90.    };
  91.  
  92. struct ExtNewScreen newscreen =
  93.    {
  94.    0,0,                           /* left & top edges */
  95.    0,0,0,                        /* width, height, & depth (filled) */
  96.    1,2,                           /* detail & block pens */
  97.    0,                              /* view modes (filled) */
  98.    CUSTOMSCREEN|SCREENQUIET|AUTOSCROLL|
  99.    NS_EXTENDED,                  /* type */
  100.    &topaz8,                        /* text attribute for font */
  101.    NULL,                           /* screen title */
  102.    NULL,                           /* ptr to gadgets */
  103.    NULL,                           /* ptr to bitmap */
  104.    tags                           /* addr of screen tags for V6 and later */
  105.    };
  106.  
  107. char custom_text[]="Custom Screen";
  108. char wb_text[]="WorkBench Window";
  109. char quit_text[]="Quit";
  110.  
  111. struct IntuiText item1b_it =
  112.    {
  113.    1,0,                        /* Front & Back pens (color reg #) */
  114.    JAM1,                        /* Draw mode */
  115.    0,1,                        /* left & top edge offsets */
  116.    &topaz8,                     /* ptr to font desc. (ROM font) */
  117.    quit_text,                  /* addr of actual text string */
  118.    NULL                        /* ptr to next intuitext struct */
  119.    };
  120.  
  121. struct MenuItem item1b = 
  122.    {
  123.    NULL,                        /* ptr to next menu item */
  124.    0,                           /* left edge */
  125.    FONT_HEIGHT+2,               /* top edge */
  126.    17*FONT_WIDTH,               /* width (# chars * width of font) */
  127.    FONT_HEIGHT+2,               /* height */
  128.    ITEMTEXT|
  129.    ITEMENABLED|HIGHCOMP,      /* flags */
  130.    0,                           /* mutual exclusion indicator (none) */
  131.    (APTR)&item1b_it,            /* ptr to item fill */
  132.    NULL,                        /* select fill (don't change, just highlight) */
  133.    0,                           /* command, or hotkey */
  134.    NULL,                        /* ptr to 1st subitem */
  135.    NULL                        /* ptr to next selected item (system filled) */
  136.    };
  137.  
  138. struct IntuiText item1a_it =
  139.    {
  140.    1,0,                        /* Front & Back pens (color reg #) */
  141.    JAM1,                        /* Draw mode */
  142.    0,1,                        /* left & top edge offsets */
  143.    &topaz8,                     /* ptr to font desc. (ROM font) */
  144.    custom_text,               /* addr of actual text string */
  145.    NULL                        /* ptr to next intuitext struct */
  146.    };
  147.  
  148. struct MenuItem item1a = 
  149.    {
  150.    &item1b,                     /* ptr to next menu item */
  151.    0,                           /* left edge */
  152.    0,                           /* top edge */
  153.    17*FONT_WIDTH,               /* width (# chars * width of font) */
  154.    FONT_HEIGHT+2,               /* height */
  155.    ITEMTEXT|
  156.    ITEMENABLED|HIGHCOMP,      /* flags */
  157.    0,                           /* mutual exclusion indicator (none) */
  158.    (APTR)&item1a_it,            /* ptr to item fill */
  159.    NULL,                        /* select fill (don't change, just highlight) */
  160.    0,                           /* command, or hotkey */
  161.    NULL,                        /* ptr to 1st subitem */
  162.    NULL                        /* ptr to next selected item (system filled) */
  163.    };
  164.  
  165. char menu1_text[]="Display";
  166.  
  167. struct Menu menu1 =
  168.    {
  169.    NULL,                  /* ptr to next menu struct */
  170.    0,0,                  /* left edge, top edge */
  171.    8*FONT_WIDTH,         /* width ((# chars + 1) * width of font) */
  172.    FONT_HEIGHT,         /* height (ignored by intuition) */
  173.    MENUENABLED,         /* flags */
  174.    menu1_text,            /* ptr to ASCIIZ name string */
  175.    &item1a               /* ptr to 1st menu item */
  176.    };
  177.  
  178. BitMapHeader bmh;
  179.  
  180. struct loadILBM_struct loadimg = 
  181.    {
  182.    NULL,               /* ptr to picture name string */
  183.    NULL,               /* ptr to 1st bitmap */
  184.    NULL,               /* ptr to 2nd bitmap (if any) */
  185.    color,            /* ptr to color table array */
  186.    256,               /* # colors in color table */
  187.    NULL,               /* height of image in pixels (filled by load call) */
  188.    NULL,               /* width of image in pixels (filled) */
  189.    NULL,               /* x display offset (filled) */
  190.    NULL,               /* y display offset (filled) */
  191.    NULL,               /* pic mode (filled) */
  192.    0,                  /* x load offset (from left) in bytes */
  193.    0,                  /* y load offset (from top) in rows */
  194.    ILBM_ALLOC1|ILBM_COLOR,   /* flags (alloc 1 bitmap, fill color table) */
  195.    0xff,               /* bitplane fill mask */
  196.    0xff,               /* bitplane load mask */
  197.    &bmh               /* address of BitMapHeader to fill */
  198.    };
  199.  
  200. struct NewWindow pic_win=
  201.    {
  202.    0,0,0,0,            /* left edge, top edge, width, height */
  203.    1,2,               /* detail & block pens */
  204.    CLOSEWINDOW|MENUPICK,   /* IDCMP flags */
  205.    WB_FLAGS,         /* window flags */
  206.    NULL,               /* ptr to 1st gadget */
  207.    NULL,               /* ptr to checkmark image */
  208.    NULL,               /* window title */
  209.    NULL,               /* ptr to screen */
  210.    NULL,               /* ptr to bitmap to use for refresh */
  211.    30,30,0,0,         /* min width & height, max width & height */
  212.    WBENCHSCREEN      /* window type */
  213.    };
  214.  
  215. struct NewWindow pic_win2=
  216.    {
  217.    0,0,0,0,            /* left edge, top edge, width, height */
  218.    1,2,               /* detail & block pens */
  219.    CLOSEWINDOW|MENUPICK,   /* IDCMP flags */
  220.    CUSTOM_FLAGS,      /* window flags */
  221.    NULL,               /* ptr to 1st gadget */
  222.    NULL,               /* ptr to checkmark image */
  223.    NULL,               /* window title */
  224.    NULL,               /* ptr to screen */
  225.    NULL,               /* ptr to bitmap to use for refresh */
  226.    0,0,0,0,            /* min width & height, max width & height */
  227.    CUSTOMSCREEN      /* window type */
  228.    };
  229.  
  230. /***************************************************************************/
  231.  
  232. main(argc,argv)
  233. int argc;
  234. char *argv[];
  235.  
  236. {
  237.    int err;
  238.  
  239.    if (gs_open_libs(INTUITION|DOS|GRAPHICS,0))
  240.       exit(-99);                        /* open necessary Amiga libs */
  241.    if (argc < 2)                        /* check # command line arguements */
  242.       {
  243.       DisplayBeep(NULL);               /* if error, flash the screen */
  244.       gs_close_libs();                  /* close Amiga libs */
  245.       exit(-98);                        /* 1st arg has to be name of file to display */
  246.       }
  247.    if (err=get_wb_info())               /* get workbench size, depth, & window border info */
  248.       {
  249.       DisplayBeep(NULL);               /* if error, flash the screen */
  250.       gs_close_libs();                  /* close Amiga libs */
  251.       exit(err);
  252.       }
  253.    if (argc >= 3)
  254.       pic_win.LeftEdge=atoi(argv[2]);   /* set left edge of window */
  255.    if (argc >= 4)
  256.       pic_win.TopEdge=atoi(argv[3]);   /* set top edge of window */
  257.    if (err=load_image(argv[1]))         /* load image into it's own bitmap */
  258.       {
  259.       DisplayBeep(NULL);               /* if error, flash the screen */
  260.       gs_close_libs();                  /* close Amiga libs */
  261.       exit(err);
  262.       }
  263.    printf("\n%d x %d x %d\n",bmh.w,bmh.h,bmh.nPlanes);   /* print dimensions */
  264.    if (err=display_image(argv[1]))      /* display it in a superbitmap window */
  265.       {
  266.       DisplayBeep(NULL);               /* if error, flash the screen */
  267.       gs_free_bitmap(loadimg.bitmap1);   /* free up bitmap we alloced by image load */
  268.       gs_close_libs();                  /* close Amiga libs */
  269.       exit(err);
  270.       }
  271.    gs_free_bitmap(loadimg.bitmap1);      /* free up bitmap we alloced by image load */
  272.    gs_close_libs();                     /* close Amiga libs */
  273.    exit(0);
  274. }
  275.  
  276. /***************************************************************************/
  277.  
  278. int get_wb_info()
  279.  
  280. {
  281.    struct Screen *s,screen13;      /* for polling WB attributes */
  282.  
  283.    if (IntuitionBase->LibNode.lib_Version >= 36)   /* if OS 2.0 or above */
  284.       {                           /* get info about workbench screen */
  285.       if (!(s=LockPubScreen("Workbench")))
  286.          return(-97);
  287.       left=s->WBorLeft;
  288.       right=s->WBorRight;
  289.       top=s->WBorTop;
  290.       bottom=s->WBorBottom;
  291.       barheight=s->BarHeight;
  292.       width=s->Width;
  293.       height=s->Height;
  294.       depth=s->BitMap.Depth;
  295.       UnlockPubScreen(NULL,s);
  296.       }
  297.    else      /* else use old 1.3 method of polling WB attributes */
  298.       {
  299.       if (!GetScreenData(&screen13,sizeof(struct Screen),WBENCHSCREEN,NULL))
  300.          return(-97);
  301.       left=screen13.WBorLeft;
  302.       right=screen13.WBorRight;
  303.       top=screen13.WBorTop;
  304.       bottom=screen13.WBorBottom;
  305.       barheight=screen13.BarHeight;
  306.       width=screen13.Width;
  307.       height=screen13.Height;
  308.       depth=screen13.BitMap.Depth;
  309.       }
  310.    return(0);
  311. }
  312.  
  313. /***************************************************************************/
  314.  
  315. int load_image(file)
  316. char *file;
  317.  
  318. {
  319.    loadimg.file=file;
  320.    return(gs_loadILBM(&loadimg));      /* load the ILBM file */
  321. }
  322.  
  323. /***************************************************************************/
  324.  
  325. int display_image(file)
  326. char *file;
  327.  
  328. {
  329.    int retcode=1;
  330.    
  331.    while (retcode > 0)
  332.       {
  333.       retcode=display_image_screen(file);
  334.       if (retcode > 0)
  335.          retcode=display_image_window(file);
  336.       }
  337.    return(retcode);
  338. }
  339.  
  340. /***************************************************************************/
  341.  
  342. int display_image_window(file)
  343. char *file;
  344.  
  345. {
  346.    int done=0,retcode=0;
  347.    struct IntuiMessage *msg;
  348.    struct Window *win;
  349.    struct ExecBase *SysBase=*(struct ExecBase **)4;
  350.  
  351.                                        /* now set window width & height */
  352.    pic_win.Width=(loadimg.bitmap1->BytesPerRow*8)+left+right+14;
  353.    pic_win.Height=loadimg.bitmap1->Rows+top+bottom+9;
  354.    pic_win.MaxWidth=pic_win.Width;      /* set maximum window width & height */
  355.    pic_win.MaxHeight=pic_win.Height;
  356.    pic_win.TopEdge+=barheight+1;
  357.    pic_win.BitMap=loadimg.bitmap1;      /* ptr to refresh bitmap */
  358.    if ((pic_win.Width+pic_win.LeftEdge) > width) /* make sure window fits in WB */
  359.       pic_win.Width=width-pic_win.LeftEdge;
  360.    if ((pic_win.Height+pic_win.TopEdge) > height)
  361.       pic_win.Height=height-pic_win.TopEdge;
  362.    pic_win.Title=file;                  /* title is name of file */
  363.    if (!(win=OpenWindow(&pic_win)))
  364.       return(-96);                     /* unable to open window */
  365.    if ((win->BorderRight != 18) || (win->BorderTop != 11) ||
  366.       (win->BorderLeft != 4) || (win->BorderBottom != 2))
  367.       {                                 /* make sure we got window borders correct */
  368.       right=(loadimg.bitmap1->BytesPerRow*8)+win->BorderLeft+win->BorderRight;
  369.       bottom=loadimg.bitmap1->Rows+win->BorderTop+win->BorderBottom;
  370.       WindowLimits(win,30,30,right,bottom);
  371.       SizeWindow(win,right-win->Width,bottom-win->Height);
  372.       }
  373.    SetWindowTitles(win,(UBYTE *)-1,screen_title);
  374.    item1a_it.IText=custom_text;
  375.    SetMenuStrip(win,&menu1);            /* tack on the menu */
  376.    while (!done)
  377.       {
  378.       WaitPort(win->UserPort);         /* wait for Intuition message */
  379.       msg=(struct IntuiMessage *)GetMsg(win->UserPort);
  380.       switch (msg->Class)
  381.          {
  382.          case CLOSEWINDOW:
  383.             done=1;
  384.             break;
  385.          case MENUPICK:
  386.             switch (ITEMNUM(msg->Code))
  387.                {
  388.                case 0:
  389.                   retcode=1;
  390.                case 1:
  391.                   done=1;
  392.                   break;
  393.                default:
  394.                   break;
  395.                }
  396.             break;
  397.          default:
  398.             break;
  399.          }
  400.       ReplyMsg((struct Message *)msg);   /* tell Intuition we're done with msg, thanx */
  401.       }
  402.    SetWindowTitles(win,(UBYTE *)-1,NULL);
  403.    ClearMenuStrip(win);                  /* remove menu strip */
  404.    CloseWindow(win);
  405.    pic_win.TopEdge-=barheight+1;
  406.    return(retcode);
  407. }
  408.  
  409. /***************************************************************************/
  410.  
  411. int display_image_screen(file)
  412. char *file;
  413.  
  414. {
  415.    int done=0,retcode=0,cnt,colors,r,g,b;
  416.    struct IntuiMessage *msg;
  417.    struct Screen *screen;
  418.    struct Window *win;
  419.    struct ExecBase *SysBase=*(struct ExecBase **)4;
  420.  
  421.                                        /* now set window width & height */
  422.    tags[1].ti_Data=loadimg.bitmap1->BytesPerRow*8;
  423.    tags[2].ti_Data=loadimg.bitmap1->Rows+barheight;
  424.    tags[3].ti_Data=loadimg.bitmap1->Depth;
  425.    newscreen.Width = loadimg.bitmap1->BytesPerRow*8;
  426.    newscreen.Height = loadimg.bitmap1->Rows+barheight;
  427.    newscreen.Depth = loadimg.bitmap1->Depth;
  428.    newscreen.ViewModes = loadimg.modes;
  429.    if (!(screen=(struct Screen *)OpenScreen((struct NewScreen *)&newscreen)))
  430.       {
  431.       DisplayBeep(NULL);
  432.       return(1);                        /* if can't open screen, go back to WB window */
  433.       }
  434.    pic_win2.Width=(loadimg.bitmap1->BytesPerRow*8);
  435.    pic_win2.Height=loadimg.bitmap1->Rows+barheight;
  436.    pic_win2.Screen = screen;
  437.    pic_win2.BitMap=loadimg.bitmap1;      /* ptr to refresh bitmap */
  438.    if (!(win=OpenWindow(&pic_win2)))
  439.       {
  440.       CloseScreen(screen);
  441.       DisplayBeep(NULL);
  442.       return(1);                        /* unable to open window */
  443.       }
  444.    colors=1<<loadimg.bitmap1->Depth;
  445.    if (IntuitionBase->LibNode.lib_Version >= 39)   /* if OS 3.0 or above */
  446.       {
  447.       for (cnt=0; cnt < colors; cnt++)
  448.          {
  449.          r=(color[cnt]&0x00ff0000)<<8;
  450.          g=(color[cnt]&0x0000ff00)<<16;
  451.          b=(color[cnt]&0x000000ff)<<24;
  452.          SetRGB32(&screen->ViewPort,cnt,r,g,b);
  453.          }
  454.       }
  455.    else
  456.       {
  457.       for (cnt=0; cnt < colors; cnt++)
  458.          {
  459.          r=(color[cnt]>>20)&0xf;
  460.          g=(color[cnt]>>12)&0xf;
  461.          b=(color[cnt]>>4)&0xf;
  462.          SetRGB4(&screen->ViewPort,cnt,r,g,b);
  463.          }
  464.       }
  465.    SetWindowTitles(win,screen_title,(UBYTE *)-1);
  466.    item1a_it.IText=wb_text;
  467.    SetMenuStrip(win,&menu1);            /* tack on the menu */
  468.    while (!done)
  469.       {
  470.       WaitPort(win->UserPort);         /* wait for Intuition message */
  471.       msg=(struct IntuiMessage *)GetMsg(win->UserPort);
  472.       switch (msg->Class)
  473.          {
  474.          case CLOSEWINDOW:
  475.             done=1;
  476.             break;
  477.          case MENUPICK:
  478.             switch (ITEMNUM(msg->Code))
  479.                {
  480.                case 0:
  481.                   retcode=1;
  482.                case 1:
  483.                   done=1;
  484.                   break;
  485.                default:
  486.                   break;
  487.                }
  488.             break;
  489.          default:
  490.             break;
  491.          }
  492.       ReplyMsg((struct Message *)msg);   /* tell Intuition we're done with msg, thanx */
  493.       }
  494.    SetWindowTitles(win,(UBYTE *)-1,NULL);
  495.    ClearMenuStrip(win);                  /* remove menu strip */
  496.    CloseWindow(win);
  497.    CloseScreen(screen);
  498.    return(retcode);
  499. }
  500.